import sqlite3
from flask import g

def get_db():
    """Get a database connection. The connection is unique for each request and will be reused if this is called again."""
    if 'db' not in g:
        g.db = sqlite3.connect('packages.db')
        g.db.row_factory = sqlite3.Row
    return g.db

def close_db(e=None):
    """Close the database connection."""
    db = g.pop('db', None)
    if db is not None:
        db.close() 